package View;

import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.xml.crypto.Data;
import javax.xml.soap.SAAJResult;
import javax.swing.JTable;
import javax.swing.JButton;

import Communication.Client;
import Communication.MessageHandle;
import DB.CustomerQuery;
import Entity.Cities;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/** this class provide the GUI window for add new customer*/
public class AddNewCustomerView extends JFrame  implements Serializable {

	private static final long serialVersionUID = 42L;

	/**required  fields and buttons */
	JTextField IdField;
	JTextField firstNameField;
	JComboBox AgeComboBox;
	JComboBox CityComboBox;
	JLabel lblPhoneNumber;
	JTable table;
	JTextField PhoneField;
	JButton AddBtn;
	JTextField lastNameField;
	JButton BackBtn;
	Client client;

	/** default and only constructor */
	public AddNewCustomerView() 
	{
		getContentPane().setLayout(null);
		setResizable(false);
		this.setTitle("AddNewCustomer");
		this.setBounds(450, 200,463, 365);
		setResizable(false);

		JLabel lblCustumerId = new JLabel("CustomerID");
		lblCustumerId.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblCustumerId.setBounds(48, 39, 109, 20);
		getContentPane().add(lblCustumerId);

		IdField = new JTextField();
		IdField.setBounds(214, 40, 135, 20);
		getContentPane().add(IdField);
		IdField.setColumns(10);

		JLabel lblName = new JLabel("First Name");
		lblName.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblName.setBounds(48, 73, 79, 15);
		getContentPane().add(lblName);

		firstNameField = new JTextField();
		firstNameField.setColumns(10);
		firstNameField.setBounds(214, 71, 135, 20);
		getContentPane().add(firstNameField);

		JLabel lblAg = new JLabel("Year of Birth");
		lblAg.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblAg.setBounds(48, 145, 109, 14);
		getContentPane().add(lblAg);

		JLabel lblLivingArea = new JLabel("City");
		lblLivingArea.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblLivingArea.setBounds(48, 181, 79, 17);
		getContentPane().add(lblLivingArea);

		/**Gets the current date and time */
		Calendar now = Calendar.getInstance();
		int year = now.get(Calendar.YEAR);

		int x=year-1950;
		String[] listOfElements = new String[x];

		for(int i=0;i<x;i++)
			listOfElements[i] = Integer.toString(i+1950);

		AgeComboBox = new JComboBox(listOfElements);
		AgeComboBox.setBounds(214, 144, 135, 18);
		AgeComboBox.addActionListener(AgeComboBox);
		getContentPane().add(AgeComboBox);

		lblPhoneNumber = new JLabel("Phone Number");
		lblPhoneNumber.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblPhoneNumber.setBounds(48, 224, 109, 16);
		getContentPane().add(lblPhoneNumber);

		table = new JTable();
		table.setBounds(222, 239, 141, -41);
		getContentPane().add(table);

		PhoneField = new JTextField();
		PhoneField.setBounds(214, 223, 135, 20);
		getContentPane().add(PhoneField);
		PhoneField.setColumns(10);

		AddBtn = new JButton("Add");
		AddBtn.setFont(new Font("Tahoma", Font.BOLD, 15));
		AddBtn.setBounds(239, 270, 135, 28);
		getContentPane().add(AddBtn);

		JLabel lblLastName = new JLabel("Last Name");
		lblLastName.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblLastName.setBounds(45, 111, 82, 14);
		getContentPane().add(lblLastName);

		lastNameField = new JTextField();
		lastNameField.setBounds(214, 109, 135, 20);
		getContentPane().add(lastNameField);
		lastNameField.setColumns(10);

		BackBtn = new JButton("Back");
		BackBtn.setFont(new Font("Tahoma", Font.BOLD, 15));
		BackBtn.setBounds(53, 270, 135, 28);
		getContentPane().add(BackBtn);

		CityComboBox = new JComboBox(Cities.getCitiesStringArr());
		CityComboBox.addActionListener( CityComboBox);
		CityComboBox.setBounds(214, 180, 135, 20);
		getContentPane().add(CityComboBox);
	}

	/**
	 * add listeners to add and back buttons function
	 * @param actionListener
	 */
	public void addNewCustomerListener(ActionListener actionListener)
	{
		AddBtn.addActionListener(actionListener);
		BackBtn.addActionListener(actionListener);	
	}

	/**show and hide window functions */
	public void showWindow()
	{
		setVisible(true);
	}
	public void hideWindow()
	{
		setVisible(false);
	}

	/**set city function*/
	public void setCitys(String[] listOfCities)
	{
		CityComboBox = new JComboBox<String>(listOfCities);
		CityComboBox.addActionListener(CityComboBox);
	}

	/**get buttons functions */
	public JButton getAddBtn()
	{
		return this.AddBtn;
	}

	public JButton getBackBtn()
	{
		return this.BackBtn;
	}

	/** get text fields function */
	public String getFirstName()
	{
		return firstNameField.getText();
	}
	public String getlastName()
	{
		return lastNameField.getText();
	}
	public String getPhone()
	{
		return PhoneField.getText();
	}
	public String getID() 
	{
		return IdField.getText();
	}
	public String getCity()
	{
		return (String) CityComboBox.getSelectedItem();
	}


	/**get combo boxes functions */
	public String getAge() 
	{
		return (String) AgeComboBox.getSelectedItem();
	}

	/**Function which clear fields */
	public void clearFields()
	{
		PhoneField.setText("");
		AgeComboBox.setSelectedItem("");
		lastNameField.setText("");
		firstNameField.setText("");
		IdField.setText("");
		CityComboBox.setSelectedItem(" ");	
	}

	/**Function which clear id field */
	public void  setID()
	{
		IdField.setText("");
	}
}